package com.jiepos.mpos.biz.user.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.jiepos.mpos.biz.user.service.UserRoleService;
import com.jiepos.mpos.biz.user.service.UserService;
import com.jiepos.mpos.common.entity.App_UserRole_InfEntity;
import com.jiepos.mpos.common.entity.App_User_InfEntity;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.Pagination;

/**
 * @ClassName: UserController
 * @Description: 用户管理控制层
 * @author 孙永
 * @date 2017年9月24日 下午1:33:03
 */
@Controller
@RequestMapping("userinf")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private UserRoleService userRoleService;
	
	/**
	 * 用户管理页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getUserInfList.do")
	public ModelAndView getUserInfList(HttpServletRequest request, HttpServletResponse response){
		ModelAndView model = new ModelAndView("/module/user/user/userinf_list");
		return super.getDetail(request, response, model);
	}

	/**
	 * 获取列表数据
	 * @param request
	 * @param response
	 * @param pagination
	 * @param condition 查询条件
	 * @return
	 */
	@RequestMapping("/getUserInfListJson.do")
	@ResponseBody
	public Map<String, Object> getUserInfListJson(HttpServletRequest request, HttpServletResponse response, 
			Pagination pagination, App_User_InfEntity condition, String roleName,String ins_cd,String ins_cn_nm){
		return userService.getUserInfPageList(request, pagination, condition, roleName,ins_cd,ins_cn_nm, super.getLoginUser(request, response));
	}
	
	/**
	 * 详情页面
	 * @param request
	 * @param response
	 * @param uuid
	 * @return
	 */
	@RequestMapping("/getUserInfDetail.do")
	public ModelAndView getUserInfDetail(HttpServletRequest request, HttpServletResponse response, String uuid){
		ModelAndView model = new ModelAndView("/module/user/user/userinf_detail");
		model.addObject("entity", userService.getUserInfByUuid(uuid));
		return super.getDetail(request, response, model);
	}
	
	/**
	 * 保存用户
	 * @param request
	 * @param response
	 * @param entity
	 * @return
	 */
	@RequestMapping("/saveUserInf.do")
	@ResponseBody
	public boolean saveUserInf(HttpServletRequest request, HttpServletResponse response, App_User_InfEntity entity) {
		userService.saveUserInf(entity, super.getLoginUser(request, response));
//		Map map=super.getLoginUser(request, response);
		return true;
//		return new ModelAndView(new RedirectView("getUserInfList.do"), map);
	}
	
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @param user_uuids 用户标识
	 * @return
	 */
	@RequestMapping("/deleteUserByIds.do")
	public ModelAndView deleteUserByIds(HttpServletRequest request, HttpServletResponse response, String user_uuids) {
		userService.deleteUserByIds(user_uuids);
		return new ModelAndView(new RedirectView("getUserInfList.do"), super.getLoginUser(request, response));
	}
	
	/**
	 * 验证登陆账号是否存在
	 * @param loginName 登陆账号
	 * @return
	 */
	@RequestMapping("/checkLoginName.do")
	@ResponseBody
	public int checkLoginName(String loginName, String phone, String uuid){
		return userService.isExist(loginName, phone, uuid);
	}
	
	/**
	 * 启用停用用户
	 * @param request
	 * @param response
	 * @param user_uuids
	 * @param isLocked
	 * @return
	 */
	@RequestMapping("/changeUserStatus.do")
	@ResponseBody
	public void changeUserStatus(HttpServletRequest request, HttpServletResponse response, String user_uuids, boolean isLocked){
		Map map = super.getLoginUser(request, response);
		userService.changeUserStatus(user_uuids, isLocked, map);
	}
	
	/**
	 * 密码重置
	 * @param request
	 * @param response
	 * @param user_uuids
	 * @return
	 */
	@RequestMapping("/resetPwd.do")
	@ResponseBody
	public void resetPwd(HttpServletRequest request, HttpServletResponse response, String user_uuids){
		Map map = super.getLoginUser(request, response);
		userService.resetPwd(user_uuids, map);
	}

	/**
	 * 保存用户角色数据
	 * @param request
	 * @param response
	 * @param entity
	 * @return
	 */
	@RequestMapping(value="/saveUserRole.do")
	@ResponseBody
	public boolean saveUserRole(HttpServletRequest request, HttpServletResponse response, App_UserRole_InfEntity entity){
		return userRoleService.saveUserRole(entity, super.getLoginUser(request, response));
	}
}
